// 已在LeetCode上通过
package main

import "fmt"

// Definition for singly-linked list.
type ListNode struct {
	Val  int
	Next *ListNode
}

func reverseBetween(head *ListNode, left int, right int) *ListNode {
	var t1, t2, t3 *ListNode = &ListNode{}, &ListNode{}, &ListNode{}
	t1.Next = head
	t2 = head
	t3 = head

	if left == right {
		return head
	}

	n := 1
	for {
		if n > left && n <= right {
			t3 = t2.Next
			t2.Next = t2.Next.Next
			t3.Next = t1.Next
			t1.Next = t3
			if n == right {
				break
			}
			n = n + 1
			continue
		}
		if n < left {
			t1 = t1.Next
			n = n + 1
			continue
		}
		if n == left {
			t2 = t1.Next
			n = n + 1
			continue
		}
		break
	}
	if left == 1 {
		head = t1.Next
	}

	return head
}

func main() {
	var s ListNode
	s = ListNode{
		Val: 3,
		Next: &ListNode{
			Val:  5,
			Next: nil,
		},
	}

	r := reverseBetween(&s, 1, 2)
	for {
		fmt.Println(r.Val)
		if r.Next == nil {
			break
		}
		r = r.Next
	}

}
